#include <iostream>
#include <vector>
#include <csignal>

#include "../../comm/httplib.h"
#include "./file_server.hpp"

using namespace httplib;
using namespace ns_file;
void signal_handler(int signal)
{
    if (signal == SIGTERM)
    {
        std::exit(0);
    }
}

int main()
{
    std::signal(SIGTERM, signal_handler);
    Server svr;

    // 业务1. 获取小说封面图片
    svr.Get(R"(/book_image/(\d+)/cover.jpg)", [](const Request &req, Response &resp)
            {   
                std::string path_name = "." + req.path;
                myFile file(path_name, 1);
                std::string content_type = file._content_type;
                content_type += " charset=utf-8";
                resp.set_content(file._data, content_type.c_str()); });

    // 业务2. 获取css文件
    svr.Get(R"(/css/(\w+)/style.css)", [](const Request &req, Response &resp)
            { 
                std::string path_name = "." + req.path;
                myFile file(path_name, 1);
                std::string content_type = file._content_type;
                content_type += " charset=utf-8";
                resp.set_content(file._data, content_type.c_str()); });

    // 业务3. 获取js文件
    svr.Get(R"(/js/(\w+)/script.js)", [](const Request &req, Response &resp)
            { 
                std::string path_name = "." + req.path;
                myFile file(path_name, 1);
                std::string content_type = file._content_type;
                content_type += " charset=utf-8";
                resp.set_content(file._data, content_type.c_str()); });

    // 业务4. 获取相关通用图片
    svr.Get(R"(/comm_image/([^/]+))", [](const Request &req, Response &resp)
            {   
                std::string image_name = req.matches[1];
                std::string path_name = "./comm_image/" + image_name;
                myFile file(path_name, 1);
                std::string content_type = file._content_type;
                content_type += " charset=utf-8";
                resp.set_content(file._data, content_type.c_str()); });
    svr.listen("0.0.0.0", 8083);

    return 0;
}